import { defineStore } from 'pinia';

export const useMainStore = defineStore('main', {
    state: () => {
        return {
            isCollpase: false,
            tags: [{
                path: '/home',
                name: 'home',
                label: '首页',
                icon: 'home'
            }],
            currentMenu: null,
            menuList: [],
            token: '',
            routerList: [],
            permission: []
        };
    },
    actions: {
        changeIsCollpase() {
            this.isCollpase = !this.isCollpase
        },
        setPermission(val) {
            this.permission = val
            localStorage.setItem('permission', JSON.stringify(this.permission))
        },
        getPermission() {
            this.permission = JSON.parse(localStorage.getItem('permission'))
            console.log('获取权限啦', this.permission);
        },
        clearPermission() {
            this.permission = []
            localStorage.removeItem('permission')
        },
        setToken(val) {
            this.token = val
            localStorage.setItem('token', this.token)
        },
        clearToken() {
            this.token = ''
            localStorage.removeItem('token')
        },
        getToken() {
            this.token = localStorage.getItem('token')
        },
        selectMenu(val) {
            if (val.name == 'home') {
                this.currentMenu = null
            } else {
                let index = this.tags.findIndex(item => item.name == val.name)
                index == -1 ? this.tags.push(val) : ''
            }
        },
        deleteMenu(tag) {
            let index = this.tags.findIndex(item => item.name == tag.name)
            this.tags.splice(index, 1)
        },
        updateMenu(val) {
            this.menuList = val
            localStorage.setItem('menu', JSON.stringify(val))
        },
        cleanMenu() {
            this.menuList = []
            localStorage.removeItem('menu')
        },
        saveMenu() {
            if (!localStorage.getItem('menu')) {
                return
            } else {
                const menu = JSON.parse(localStorage.getItem('menu'))
                this.menuList = menu

            }
        },
        addMenu(router) {
            const menu = this.menuList
            const module = import.meta.glob('../view/**/*.vue');
            console.log('module', module);
            const routeArr = []
            menu.forEach(item => {
                if (item.children) {
                    item.children.forEach(val => {
                        let url = `../views/${val.url}.vue`
                        val.component = module[url]
                        console.log('1111', item.children);
                        routeArr.push(...item.children)
                    })
                } else {
                    let url = `../views/${item.url}.vue`
                    item.component = module[url]
                    console.log('2222', item);
                    routeArr.push(item)
                }
            })
            let routers = router.getRoutes()
            routers.forEach(item => {
                if (item.name == 'main' || item.name == 'login') {
                    return
                } else {
                    router.removeRoute(item.name)
                }
            })

            routeArr.forEach(item => {
                this.routerList.push(router.addRoute("main", item))
            })
        }
    },
});